package DataBase;


import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Types;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Vector;

import DataObjects.StatusOfTable;

public class GetStatusOfAllTables {

	private Vector<StatusOfTable> resTableStatus=new Vector<StatusOfTable>();
	
	Date date;
	Calendar cal;
	
	public void get()
	{
		SQLConnection sc=new SQLConnection();
		
		try {
			

		PreparedStatement ps = sc.getConnection().prepareStatement("SELECT TT.tid, TT.dateOpen, E.name,TT.timeOpen,TT.orderId,TT.no_of_persons FROM Table_TimeSlot TT, Employee E, Employee_Table_TimeSlot ETT WHERE TT.status='opened' AND ETT.tId=TT.tId AND ETT.eid=E.eid;");	

		ResultSet rs=ps.executeQuery();	

		ResultSetMetaData rsmd = rs.getMetaData();

	    resTableStatus = new Vector<StatusOfTable>();
		
		 while(rs.next())
	        {   

			 StatusOfTable  table = new StatusOfTable();
			 table.settId(rs.getString(1));
			 table.setNameOfOfficiant(rs.getString(3));
			 table.setNoOfPersons(rs.getInt(6));
			 
			//setting time and date of opening table 
			String timeStr=rs.getString(4);
			String dateStr=rs.getString(2);
			//String dateStr=rs.getTimestamp(2).toString();
			int day=Integer.parseInt(dateStr.substring(8,10));
			int month=Integer.parseInt(dateStr.substring(5, 7))-1;      // month range 0-11
			int year=Integer.parseInt(dateStr.substring(0, 4));
			int hour=Integer.parseInt(timeStr.substring(0, 2));
		    int minute=Integer.parseInt(timeStr.substring(3, 5));
			int second=Integer.parseInt(timeStr.substring(6, 8));					
			cal = Calendar.getInstance();
			cal.clear();		
			cal.set(Calendar.HOUR_OF_DAY,hour);
			cal.set(Calendar.MINUTE,minute);
			cal.set(Calendar.SECOND,second);
			cal.set(Calendar.MILLISECOND,0);
			cal.set(Calendar.YEAR,year);
			cal.set(Calendar.MONTH,month);
			cal.set(Calendar.DAY_OF_MONTH,day);
			table.setCal(cal);
			table.setOrderId(rs.getInt(5));
			resTableStatus.add(table);
					
	        }//while
		    ps.close();
			rs.close();
			sc.getConnection().close();
		
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
	}
	public Vector<StatusOfTable> getResTableStatus() {
		return resTableStatus;
	}
}
